package com.dgut.coal.controller;

import com.dgut.coal.entity.Margin;
import com.dgut.coal.service.MarginService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/margin")
public class MarginController {
    @Autowired
    MarginService marginService;

    /**
     * 查询报价保证金和履约保证金列表
     * @param marginType
     * @param pageNum
     * @param map
     * @param bill_number
     * @param supplier_name
     * @param marginState
     * @return
     */
    @RequestMapping("/list")
    public String marginIndex(Integer marginType, @RequestParam(required = false,defaultValue = "1",value = "pn") Integer pageNum, Map<String, Object> map
            ,@RequestParam(required = false,defaultValue = "",value = "bill_number") String bill_number
            ,@RequestParam(required = false,defaultValue = "",value = "supplier_name")String supplier_name
            ,@RequestParam(required = false,defaultValue ="-1",value = "marginState")Integer marginState
    ){
        PageHelper.startPage(pageNum,5);
        List<Margin> marginList = marginService.findAllByParam(marginType,bill_number,supplier_name,marginState);
        PageInfo pageInfo = new PageInfo<>(marginList);
        List keywords = Arrays.asList(bill_number, supplier_name, marginState);
        map.put("keywords", keywords);
        map.put("pageInfo",pageInfo);
        if(marginType == 1){
            return "margin-baojia-list";
        }else {
            return "margin-lvyue-list";
        }
    }

    /**
     * 查询保证金审核列表
     * @param pageNum
     * @param map
     * @param bill_number
     * @param supplier_name
     * @return
     */
    @RequestMapping("/auditList")
    public String marginAuditList(@RequestParam(required = false,defaultValue = "1",value = "pn") Integer pageNum, Map<String, Object> map
            ,@RequestParam(required = false,defaultValue = "",value = "bill_number") String bill_number
            ,@RequestParam(required = false,defaultValue = "",value = "supplier_name")String supplier_name
    ){
        PageHelper.startPage(pageNum,5);
        List<Margin> marginList = marginService.findAllAuditByParam(bill_number,supplier_name);
        PageInfo pageInfo = new PageInfo<>(marginList);
        List keywords = Arrays.asList(bill_number, supplier_name);
        map.put("keywords", keywords);
        map.put("pageInfo",pageInfo);
        return "margin-audit-list";
    }

    /**
     * 根据id查询保证金信息并返回响应页面
     * @param marginId
     * @param tag
     * @param modelAndView
     * @return
     */
    @RequestMapping("/view")
    public ModelAndView marginView(Integer marginId,Integer tag, ModelAndView modelAndView){
        System.out.println("ID"+marginId);
        Margin margin = marginService.findMarginById(marginId);
        System.out.println(margin);
        modelAndView.addObject("margin",margin);
        if(tag == 0){
            modelAndView.setViewName("margin-baojia-view");
        }else if (tag == 1){
            modelAndView.setViewName("margin-baojia-thaw");
        }else {
            modelAndView.setViewName("margin-audit");
        }
        return modelAndView;
    }

    /**
     * 解冻
     * @param marginId
     * @param remark
     * @return
     */
    @RequestMapping("marginThaw")
    @ResponseBody
    public int marginThawWay(@Param("marginId") Integer marginId, @Param("remark")String remark){
        System.out.println("参数ss："+marginId+","+remark);
        marginService.marginThaw(marginId,remark);
        return 5;
    }
//    @RequestMapping("marginThaw")
//    public String marginThawWay(Integer marginId,String remark){
//        System.out.println("参数ss："+marginId+","+remark);
//        return "";
//    }

    /**
     * 审核
     * @param marginId
     * @return
     */
    @RequestMapping("marginAudit")
    @ResponseBody
    public int marginAuditWay(@Param("marginId") Integer marginId,@Param("state") Integer state){
        System.out.println("参数aduit："+marginId);
        System.out.println("参数state："+state);
        marginService.marginAudit(marginId,state);
        return 5;
    }
}
